import {BizKfExtraProColumnType} from '@/components';
import * as API from '@/services';
import {Divider, message, Space, Typography} from 'antd';
import {KfModal, KfPagingTablePage, KfPagingTablePagePropsAction, useBackendEnum} from 'kf-web-library';
import React, {useRef} from 'react';
import {useLocation, useAccess} from 'umi';
import QuickForm from './components/QuickForm';
import "./index.less"
import numeral from "numeral";

type Entity = API.RefundRecord;
type EntityPageQueryParams = API.RefundRecordPageQueryParams;
const entityTypeName = '退订记录';
const EntityQuickForm = QuickForm;

interface EntityTableListProps {
}

/**
 * 订阅记录 列表页面
 */
const EntityTableList: React.FC<EntityTableListProps> = (props) => {
  const location = useLocation();
  const access = useAccess();
  const {enumDict, enumTypeDict, getBackendEnum} = useBackendEnum();
  const actionRef = useRef<KfPagingTablePagePropsAction<Entity, EntityPageQueryParams>>();

  return (
    <KfPagingTablePage<Entity, EntityPageQueryParams, BizKfExtraProColumnType>
      actionRef={actionRef}
      entityTypeName={entityTypeName}
      pagingQuery={API.queryRefundRecords}
      opActionsDef={{
        single: {
          deleteOne: (record) => {
            KfModal.deleteConfirm(record.id, () => {
              API.deleteRefundRecord(record.id!).then((res) => {
                if (res.flag) {
                  message.info('删除成功');
                  actionRef.current?.reload();
                }
              });
            });
          },
        },
        multi: {
          deleteMultiple: (records) => {
            KfModal.batchActionConfirm('删除', records.length, () => {
              API.deleteRefundRecords(records.map((r) => r.id!)).then((res) => {
                if (res.flag) {
                  message.info('批量删除成功');
                  actionRef.current?.reload();
                }
              });
            });
          },
        },
      }}
      opFloatingsDef={{
        noargs: {
          createModal: () => ({
            title: `新建`,
            content: <EntityQuickForm formType={'create'}/>,
          }),
        },
        single: {
          modifyModal: (record) => ({
            title: `编辑`,
            content: <EntityQuickForm formType={'modify'} bizId={record.id}/>,
          }),
          viewModal: (record) => ({
            fltType: 'modal',
            title: `查看`,
            content: <EntityQuickForm formType={'view'} bizId={record.id}/>,
          }),
        },
      }}
      proTable={{
        tableBarActions: (action, rows) => {
          return [];
        },
        search: {
          optionRender: (searchConfig, formProps, dom) => [
            ...dom,
          ],
        },
      }}
      columns={[
        {
          title: '关键字',
          dataIndex: 'searchText',
          sorter: false,
          hideInTable: true,
          hideInForm: true,
          fieldProps: {
            placeholder: '名称和编码',
          },
        },
        {
          title: '退订日期',
          width: 200,
          dataIndex: nameof<Entity>((x) => x.refundTime),
          sorter: false,
          hideInSearch: true,
        },
        {
          title: '订单',
          dataIndex: nameof.split<Entity>((x) => x.order.id),
          width: 200,
          sorter: false,
          hideInSearch: true,
          ellipsis: true,
          render: (text, record) => {
            return (
              <div>
                <div>{record.order?.orderNo}</div>
                <div>订阅载体: {getBackendEnum('SubscribeDisplayType', record.order?.displayType)?.i18EnumDesc}</div>
                <div>订阅刊时: {getBackendEnum('SubscribeTimeType', record.order?.timeType)?.i18EnumDesc}</div>
              </div>
            );
          },
        },
        {
          title: '用户',
          width: 150,
          dataIndex: nameof.split<Entity>((x) => x.order.user.nickname),
          sorter: false,
          hideInSearch: true,
          render: (text, record) => {
            return (
              <div>
                <div>{record.order?.user?.nickname}</div>
                {record.order?.user?.phone && (<div>{record.order?.user?.phone}</div>)}
                {record.order?.user?.email && (<div>{record.order?.user?.email}</div>)}
              </div>
            )
          },
        },
        {
          title: '订阅期刊',
          dataIndex: nameof.split<Entity>((x) => x.order.journal.journalNameCn),
          sorter: false,
          hideInSearch: true,
          render: (text, record) => {
            const ret = `共订阅${record.order?.periodCount}期，已交付${record.order?.periodCount}期`
            return (
              <div>
                <div>{record.order?.journal?.journalNameCn}</div>
                <div>{ret}</div>
              </div>
            )
          }
        },
        {
          title: '支付金额',
          width: 90,
          dataIndex: nameof.split<Entity>((x) => x.order.totalPrice),
          sorter: false,
          hideInSearch: true,
          render: (text,record) => {
            return <>￥{numeral(record.order?.totalPrice / 100).format('0.00')}</>
          }
        },
        {
          title: '退款金额',
          width: 90,
          dataIndex: nameof<Entity>((x) => x.refundAmount),
          sorter: false,
          hideInSearch: true,
          render: (text,record) => {
            return <div>
              <div>￥{numeral(record.refundAmount / 100).format('0.00')}</div>
            </div>
          }
        },
        {
          title: '退款状态',
          width: 100,
          dataIndex: nameof<Entity>((x) => x.refundStatus),
          sorter: false,
          hideInSearch: true,
          render: (text,record) => {
            return <div>
              <div>{getBackendEnum('PayStatus', record.refundStatus)?.i18EnumDesc}</div>
            </div>
          }
        }
      ]}
    />
  );
};

export default EntityTableList;
